/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.jsp.JspWriter;
import util.Constants;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.Projectstatus;
import session.ProjectstatusFacadeRemote;

/**
 *
 * @author TIT
 */
public class projectstatusController {

    public static ProjectstatusFacadeRemote getProjectstatusFacade() throws NamingException {
        return util.Facade.getProjectStatusFacade();
    }

    /**
     * Get Action Link HTML code to insert into JSP file.
     */
    public static String getActionLink(String id) {
        String edit = "<a onclick=\"doAction(frmIndex, '" + Constants.ADMIN_PAGE_NAME +
                "', '" + Constants.PROJECT_STATUS_PAGE_NAME + "', '" + Constants.PROJECT_STATUS_EDIT +
                "', '" + Constants.SHOW_EDIT_PAGE +"', '" + id + "')\">Edit</a>";
        String delete = "<a onclick=\"doAction(frmIndex, '" + Constants.ADMIN_PAGE_NAME +
                "', '" + Constants.PROJECT_STATUS_PAGE_NAME + "', '" + Constants.PROJECT_STATUS_LIST+
                "', '" + Constants.DELETE +"', '" + id + "')\">Delete</a>";
        return edit + delete;
    }

    /**
     * Show all severities on screen.
     */
    public static void showAllProjectstatus(JspWriter out) {
        Controller.log.info("showAllProjectstatus");
        try {
            List<model.Projectstatus> projectstatusesList = getProjectstatusFacade().findAll();

            /*
             */
            //Create the title
            out.println("<div id='list-table'>");
            out.println("<div class='row'>");
            out.println("<div class='column-header' style='width: " +
                    Constants.ID_COLUMN_SIZE +";'>ID</div>");
            out.println("<div class='column-header' style='width: " +
                    Constants.OBJECT_NAME_COLUMN_SIZE +";'>Name</div>");
            out.println("<div class='column-header' style='width: " +
                    Constants.OBJECT_DESCRIPTION_COLUMN_SIZE +";'>Description</div>");
            out.println("<div class='column-header' style='width: " +
                    Constants.ACTION_COLUMN_SIZE +";'>Action</div>");
            out.println("<div class='clear'></div>");
            out.println("</div>");

            //Show Values
            for(int i = 0; i < projectstatusesList.size(); i++) {
                Projectstatus node = projectstatusesList.get(i);
                out.println("<div class='row'>");
                out.println("<div class='column' style='width: " +
                        Constants.ID_COLUMN_SIZE +";'>" +
                        node.getProjectStatusID() + "</div>");
                out.println("<div class='column' style='width: " +
                        Constants.OBJECT_NAME_COLUMN_SIZE +";'>" +
                        node.getName() + "</div>");
                out.println("<div class='column' style='width: " +
                        Constants.OBJECT_DESCRIPTION_COLUMN_SIZE +";'>" +
                        node.getDescription() +"</div>");
                out.println("<div class='column' style='width: " +
                        Constants.ACTION_COLUMN_SIZE +";'>" +
                    getActionLink(node.getProjectStatusID()) +"</div>");
                out.println("</div>");
                out.println("<div class='clear'></div>");
            }
            out.println("</div>");

            Controller.log.info("showAllProjectstatus finish.");
        } catch(Exception ex) {
            Controller.log.error("Error showAllProjectstatus: " + ex.getMessage());
        }
    }

    /**
     */
    /**
     * Process action
     */
    public static void processAction(HttpServletRequest request) {
        String action = request.getParameter("action");
        String selectedID = request.getParameter("selectedItemID");

        Controller.log.info("projectstatusController.processAction");

        try {
            if(Constants.DELETE.equalsIgnoreCase(action)) {
                //Process DELETE action.
                if((selectedID == null) || "".equals(selectedID)) {
                    //If the selectedID is null, throw an Exception.
                    throw new Exception("No selected id to delete.");
                } else {
                    //Delete selected Severity.
                    getProjectstatusFacade().deleteByID(selectedID);

                    Controller.sendMessage(request, "Deleted Projectstatus: " + selectedID, false);
                    Controller.log.info("Deleted Projectstatus: " + selectedID);
                }

            } else if(Constants.SHOW_EDIT_PAGE.equalsIgnoreCase(action)) {
                //Process SHOW_EDIT_PAGE action.
                if((selectedID == null) || "".equals(selectedID)) {
                    //If the selectedID is null, throw an Exception.
                    throw new Exception("No selected id to edit.");
                } else {
                    //Get value to send back to Edit page.
                    Projectstatus projectstatus = getProjectstatusFacade().find(selectedID);
                    if(projectstatus == null) {
                        throw new Exception("The selected Projectstatus not found.");
                    } else {
                        HttpSession session = request.getSession();
                        session.setAttribute("projectstatusID", projectstatus.getProjectStatusID());
                        session.setAttribute("name", projectstatus.getName());
                        session.setAttribute("description", projectstatus.getDescription());
                        Controller.sendMessage(request, "description: " + projectstatus.getDescription(), false);
                    }


                }

            } else if(Constants.INSERT.equalsIgnoreCase(action)) {
                //Process INSERT action.
                String projectstatusID = request.getParameter("projectstatusID");
                String name = request.getParameter("name");
                String description = request.getParameter("description");

                name = util.Unicode.changeToUnicode(name);
                description = util.Unicode.changeToUnicode(description);

                if((selectedID == null) || "".equals(projectstatusID)) {
                    //If the ID is null.
                    Controller.sendMessage(request, "Please input ID.", true);
                    throw new Exception("The input ID is null.");
                } else if((name == null) || "".equals(name)) {
                    //If the Name is null.
                    Controller.sendMessage(request, "Please input Name.", true);
                    throw new Exception("The input Name is null.");
                } else {
                    ProjectstatusFacadeRemote projectstatusBean = getProjectstatusFacade();
                    if(projectstatusBean.find(projectstatusID) != null) {
                        //If the severity id already exists.
                        Controller.sendMessage(request, "The PositionID have already exists,", true);
                        throw new Exception("The Position ID have already exists.");
                    } else {
                        //Insert new severity into DB.
                        Projectstatus temp = new Projectstatus(projectstatusID, name);
                        temp.setDescription(description);

                        projectstatusBean.insert(temp);
                    }
                }

                Controller.sendMessage(request, "Insert Successfully.", false);
                Controller.log.info("Insert Projectstatus: " + projectstatusID);

            } else if(Constants.EDIT.equalsIgnoreCase(action)) {
                //Process EDIT action.
                String projectstatusID = request.getParameter("projectstatusID");
                String name = request.getParameter("name");
                String description = request.getParameter("description");

                name = util.Unicode.changeToUnicode(name);
                description = util.Unicode.changeToUnicode(description);

                if((name == null) || "".equals(name)) {
                    //If the Name is null.
                    Controller.sendMessage(request, "Please input Name.", true);
                    throw new Exception("The input Name is null.");
                } else {
                    ProjectstatusFacadeRemote projectstatusBean = getProjectstatusFacade();
                    Projectstatus projectstatus = projectstatusBean.find(projectstatusID);
                    if(projectstatus == null) {
                        throw new Exception("Cannot found the target Position.");
                    } else {
                        projectstatus.setName(name);
                        projectstatus.setDescription(description);
                        projectstatusBean.edit(projectstatus);
                    }
                }


                Controller.sendMessage(request, "Save Successfully.", false);
                Controller.log.info("Save Projectstatus: " + projectstatusID);
            }
        } catch (Exception ex) {
            Controller.log.error("Error projectstatusController.processAction" + ex.getMessage());
        }
    }
}
